#include "MeasurE452AlgBackL1ToL8Circle.h"

MeasurE452AlgBackL1toL8Circle::MeasurE452AlgBackL1toL8Circle(int i):MeasurE452AlgBase(i)
{

}

void MeasurE452AlgBackL1toL8Circle::operator()(cv::Mat &srcImg,
                                               int id,
                                               std::map<string, float> &measRes,
                                               int fixtureId)
{
    qDebug() << "start to E452 alg back l1 to l8 circle";
    //背光图小半圆边缘提取  // CDL back图    \paras616\back\ cdL.xml
//    int NewCircleEdge(cv::Mat srcimg, const vector<int> paras, vector<cv::Point>& contours);
    std::vector<cv::Point> resptVec;

    for(auto i=0;i<8;i++)
    {
        std::vector<int> tparas;
        for(auto j=0;j< m_dataAllVec[i].m_edgeParam.cols;j++)
        {
            tparas.push_back(m_dataAllVec[i].m_edgeParam.at<int>(i,j));
        }
        //paras.push_back(tparas);
        std::vector<cv::Rect> rangVec;
        if(srcImg.empty()) {
            Q_ASSERT(0);
        }
        TemplateMatch(srcImg,m_dataAllVec[i],rangVec);

        cv::Mat imgt=srcImg(rangVec[0]).clone();
        std::vector<cv::Point> contours;
        MeasurE452EdgeDetect obj;
        obj.NewCircleEdge(imgt,tparas,contours);
        ImageCoordinateTransformation(contours,rangVec);

        std::unique_lock<std::mutex> lock(*MeasurE452AlgBase::mtxptrVec[id]);
        while (!MeasurE452AlgBase::flagVec[id])
        {
            (*MeasurE452AlgBase::cvarptrVec[id]).wait(lock);
        }
        for(auto &elem:contours)
        {
            cv::Point2f ptsIn(static_cast<float>(elem.x),static_cast<float>(elem.y)),ptsOut;
            CoordinateTransformation(datumBackVec[id],ptsIn,ptsOut);
            resptVec.push_back(ptsOut);
        }
    }

    // go on coding
    //cout<<resptVec[3].x<<"\t"<<resptVec[3].y<<endl;
    MeasurE452EdgeDetect obj;
    vector<float> cirres=obj.FitCircle(resptVec);

    measRes.insert(std::make_pair(string("FAI21-L-diam_pixel"), cirres[2]*2));
    float fai21dd = CalcMeasureDistance(m_phypixpara[fixtureId][std::string("FAI21-L-diam")], cirres[2]*2);
    measRes.insert(std::make_pair(string("FAI21-L-diam_real"), fai21dd));

    measRes.insert(std::make_pair(string("FAI22-L-X-distance_pixel"), -cirres[1]));
    float fai22dx = CalcMeasureDistance(m_phypixpara[fixtureId][std::string("FAI22-L-X-distance")], -cirres[1]);
    measRes.insert(std::make_pair(string("FAI22-L-X-distance_real"), fai22dx));

    measRes.insert(std::make_pair(string("FAI22-L-Y-distance_pixel"), -cirres[0]));
    float fai21dy = CalcMeasureDistance(m_phypixpara[fixtureId][std::string("FAI22-L-Y-distance")], -cirres[0]);
    measRes.insert(std::make_pair(string("FAI22-L-Y-distance_real"), fai21dy));

    float fai22pos=obj.GetPosition(cv::Point2f(m_standardValMap["FAI22-L-X-distance"],
                                               m_standardValMap["FAI22-L-Y-distance"]),
                                   cv::Point2f(fai22dx, fai21dy));
    measRes.insert(std::make_pair(string("FAI22-L-position"), fai22pos));

    qDebug() << "end to E452 alg back l1 to l8 circle";
}
